/*
* 案件中心
*/

'use strict'



YX.fn.caseCenter = function () {
    this.casecenterWrap = $("#casecenterWrap");
    this.caseInfoContainer = $("#case-info-container");
    this.casecenterWrap.delegate('#caseList ul li', 'click', this.showCaseInfo.bind(this));
    this.caseInfoContainer.on("click",".showCaseDetail .topSelect span",this.showCaseModule.bind(this));
    this.caseDetail = "";//定义案件详情
    this.caseToCreateteam = $("#case-to-createteam");
    this.caseToCreateteam.on("click",this.goChactBlock.bind(this));
}

//获取案件列表
YX.fn.getCaseList = function(){
    var _that = this;
    //开始调用赢火虫内部服务的接口
    var bodyInfo = {
        "pageNum":1,
        "pageSize":100,
        "status":"('1','2','3','4','5','A','B','C','D','E','F','G')",
        "beginDate":"",
        "endDate":"",
        "keyword":""
    }

    firefly_IM_request.getRequest("get","/firefly-erp/caseInfos",bodyInfo,function(data){
        console.log("案件列表=",data)
        if(data.isSuccess == "T"){
            var caseList = data.body;
            var html = "",
                flag = false;
            //开始渲染列表
            var tmp = '<div class="caseslist m-panel" id="caseList"><ul>';
            if(caseList.length > 0){
                flag = true;
                for(var i=0;i<caseList.length;i++){
                    var debtorName = caseList[i].debtorName?caseList[i].debtorName:"暂无";
                    var creditorName = caseList[i].creditorName?caseList[i].creditorName:"暂无";
                    var teamHtml = caseList[i].teamId?'<div class="cteam">'+
                                                            '<div class="spanf">群ID</div>'+
                                                            '<div class="spans">'+caseList[i].teamId+'</div>'+
                                                        '</div>':"";
                    tmp +=[
                        '<li data-type='+caseList[i].lawyerType+' data-caseId='+caseList[i].caseId+'>',
								'<div class="liTop">',
									'<img src="images/black.png" alt="">',
									'<span>'+caseList[i].applyUserName+'</span>',
									'<span class="cdate">'+dateStr(caseList[i].applyDate)+'</span>',
									'<span class="cstatus">'+caseList[i].statusDesc+'</span>',
								'</div>',
								'<div class="bygao">',
									'<p class="beigao"><img class="ybimg" src="images/bei.png" />'+debtorName+'</p>',
									'<p class="yuangao"><img class="ybimg" src="images/yuan.png" />'+creditorName+'</p>',
								'</div>',
								teamHtml,
								'<div class="caseAmt">',
									'<span class="amtf">'+(caseList[i].caseAmt/10000).toFixed(2)+'</span>',
									'<span class="amtu">万元</span>',
								'</div>',
							'</li>'
                    ].join(""); 
                }
            }
            tmp += '</ul></div>';
            if(!flag){
                html = '<div class="caseslist m-panel" id="caseList"><div class="nolist">暂未获取到案件列表 ):</div></div>'
            }else{
                html = tmp;
            }
            //插入Dom节点中
            _that.casecenterWrap.html(html);
        }else{
          console.log("_that===",_that)
          
        }
    },function(data){

    });
}

//显示案件各模块
YX.fn.showCaseModule = function(e){
    // console.log('eeee===',e.currentTarget.getAttribute("data-flag"))
    var flag = e.currentTarget.getAttribute("data-flag");
    $('.showCaseDetail .topSelect span[data-flag="' + flag + '"]')
    .addClass('slt')
    .siblings('span')
    .removeClass('slt');
    if(flag == "caseIntr"){
        $(".caseDetailInfo").removeClass("hide");
        $(".caseProgress").addClass("hide");
        $(".caseParticipant").addClass("hide");
        $(".attachmentContainer").addClass("hide");
        this.showCaseIntr();
    }else if(flag == "caseFollow"){
        $(".caseDetailInfo").addClass("hide");
        $(".caseProgress").removeClass("hide");
        $(".caseParticipant").addClass("hide");
        $(".attachmentContainer").addClass("hide");
        this.showCaseFollow();
    }else if(flag == "caseMemo"){
        $(".caseDetailInfo").addClass("hide");
        $(".caseProgress").addClass("hide");
        $(".caseParticipant").removeClass("hide");
        $(".attachmentContainer").addClass("hide");
        this.showCaseMemo();
    }else if(flag == "caseFile"){
        $(".caseDetailInfo").addClass("hide");
        $(".caseProgress").addClass("hide");
        $(".caseParticipant").addClass("hide");
        $(".attachmentContainer").removeClass("hide");
        this.showCaseFile();
    }
}

//显示案件详情模块
YX.fn.showCaseInfo = function(e,id){
    var caseId,lawyerType;
    if(e){
        caseId = e.currentTarget.getAttribute("data-caseId");
        lawyerType = e.currentTarget.getAttribute("data-type");
    }else{
        caseId = id
    }
   
    $(e.currentTarget).siblings('li').removeClass('activity');  
    $(e.currentTarget).addClass("activity")
   //隐藏其它窗口
   $("#rightPanel") && $("#rightPanel").addClass('hide');//右边聊天窗口
   $('#devices') && $('#devices').addClass('hide');//多端登录窗口
   $('#teamInfoContainer') && $('#teamInfoContainer').addClass('hide');//群资料
   $('#relevanceCaseContainer') && $('#relevanceCaseContainer').addClass('hide');//关联案件
   $('#cloudMsgContainer') && $('#cloudMsgContainer').addClass('hide');//云记录窗口
   $('#createTeamContainer') && $('#createTeamContainer').addClass('hide');//创建群组窗口
   $("#noSelectChat") && $("#noSelectChat").addClass('hide');//未选择聊天窗口
   $("#createNoTeamContainer") && $("#createNoTeamContainer").addClass('hide');//未选择群组聊天窗口
   $('#createNoCaseContainer') && $('#createNoCaseContainer').addClass('hide');//未选择案件聊天窗口

   //打开案件详情窗口
   $('#case-info-container').removeClass('hide');
   $(".caseDetailInfo").removeClass("hide");
   $(".caseProgress").addClass("hide");
   $(".caseParticipant").addClass("hide");
   $(".attachmentContainer").addClass("hide");
   $('.showCaseDetail .topSelect span[data-flag="caseIntr"]')
   .addClass('slt')
   .siblings('span')
   .removeClass('slt');
   this.requestGetCaseDetail(caseId,lawyerType)
}

//显示案情简介模块
YX.fn.showCaseIntr = function(){
   if(this.caseDetail){
    var caseDetailBean =  this.caseDetail.caseDetailBean;
    if(caseDetailBean){
        var introHtml = 
                 '<div class="caseIntroduction">'+
                     '<div class="fybgao">'+
                        '<div class="beigao"><img class="ybimg" src="images/bei.png" />'+(caseDetailBean.debtorName?caseDetailBean.debtorName:'暂无')+'</div>'+
                        '<div class="yangao"><img class="ybimg" src="images/yuan.png" />'+(caseDetailBean.creditorName?caseDetailBean.creditorName:'暂无')+'</div>'+
                        '</div>'+
                        '<div class="tip">'+caseDetailBean.statusDesc+'</div>'+
                 '</div>'+
                 '<div class="statuslabel">'+
                    (caseDetailBean.caseTypeName?'<span>'+caseDetailBean.caseTypeName+'</span>':'')+
                 '</div>'+
                 '<div class="amtdetails">'+
                     '<div class="caseli">'+
                     '<span class="spf">标的额</span>'+
                     '<span class="sps">'+(caseDetailBean.caseAmt/10000).toFixed(2)+'</span>'+
                     '<span class="spt">万元</span>'+
                     '</div>'+
                     '<div class="caseli">'+
                         '<span class="spf">申请人</span>'+
                         '<span class="spx">'+caseDetailBean.applyUserName+'</span>'+
                     '</div>'+
                     '<div class="caseli">'+
                         '<span class="spf">手机号</span>'+
                         '<span class="spx">'+formatePhone(caseDetailBean.mobileNo)+'</span>'+
                     '</div>'+
                     '<div class="caseli">'+
                         '<span class="spf">申请于</span>'+
                         '<span class="spx">'+dateStr(caseDetailBean.applyDate)+ " " +minStr(caseDetailBean.applyTime)+'</span>'+
                     '</div>'+
                 '</div>'+
                 '<div class="caseIntro">'+
                     '<p>'+(caseDetailBean.caseMemo?caseDetailBean.caseMemo:"暂无")+'</p>'+
                 '</div>';
            $(".caseDetailInfo").html(introHtml)
        }else{
            $(".caseDetailInfo").html('<div>暂无案情简介</div>')
        }
   }else{
    //    this.requestGetCaseDetail(caseId);
   }
}

//显示案件跟踪
YX.fn.showCaseFollow = function(){
    if(this.caseDetail){
        if(this.caseDetail.caseMarchResponseBean){
            var marchBeanList = this.caseDetail.caseMarchResponseBean.marchBeanList;
            var followHtml = "";
            marchBeanList.forEach(function(item,index){
                followHtml += [
                   '<div class="progressList'+(index==0&&marchBeanList.length !=1 ?' first':index==marchBeanList.length-1?' last':'')+'">',
                        '<div class="left"><div class="pointer'+(index>0?' smpointer':' first')+'"></div><div class="line"></div></div>',
                        '<div class="right">',
                            '<div class="depart">',
                                '<div class="text1"><span class="rq">'+dateStr(item.operDate)+ '</span><span class="sj">'+item.operTime+'</span>',
                            '</div>',
                            '<div class="content">'+item.trackDesc+'</div>',
                ].join("");
                if(item.docList.length > 0){
                    var docList = item.docList;
                    var tempDocHtml = '<div class="docs">';
                    for(var i =0;i<docList.length;i++){
                        tempDocHtml +=[
                                '<div class="docsList">',
                                    '<a href="'+docList[i].docUrl+'"target="_blank">',
                                        '<div class="cicon"><img src="'+filterFileSuffix(docList[i].docName)+'"></div>',
                                        '<span>'+filterOssUrl(docList[i].docUrl)+'</span>',
                                    '</a>',
                                '</div>', 
                        ].join('')
                    }
                    tempDocHtml += '</div>';
                    followHtml += tempDocHtml;
                }
                followHtml += "</div></div></div>"
            });
            
        }else{
            followHtml += '<div class="noCaseShow" style="margin-top: 90px;text-align:center">'+
                        '<span>暂无附件列表 ):</span>'+
                '</div></div>'
        }
        $('.caseProgress').html(followHtml)
    }
}

//显示参与人员
YX.fn.showCaseMemo = function(){
    if(this.caseDetail){
        var caseLawyerBeans = this.caseDetail.caseLawyerBeans;
        var yhcTeamList =[],clientList = [], authorizationList = [];
        var memoBlockHtml = "";
        if(caseLawyerBeans.length > 0){
            caseLawyerBeans.forEach(function(item,index){
                if(item.userBelongType == '赢火虫团队'){
                    yhcTeamList.push(item)
                }
                if(item.userBelongType == '案件授权'){
                    clientList.push(item)
                }
                if(item.userBelongType == '客户'){
                    authorizationList.push(item)
                }
            });
        }
        if(yhcTeamList.length >0){
            var yhcTeamHtml = '<div class="teamMember">'+
                                '<div class="memname">'+
                                    '<img src="images/yhctd.png" />'+
                                    '<span>赢火虫团队</span>'+
                                '</div>'+
                                '<div class="memlist">';
            yhcTeamList.forEach(function(item,index){
                yhcTeamHtml +=[
                            '<div class="mblock">',
								'<img src="'+ (item.lawyerAvatar?item.lawyerAvatar:'images/default-icon.png') +'" alt="">',
								'<div class="mdesc">',
									'<p class="pf">'+item.realName+'</p>',
									'<p class="pt">'+item.lawyerType+'</p>',
								'</div>',
							'</div>'
                ].join("");
            });
            yhcTeamHtml +='</div></div>'
            memoBlockHtml += yhcTeamHtml;
        }
        if(clientList.length >0){
            var clientHtml = '<div class="teamMember">'+
                                '<div class="memname">'+
                                    '<img src="images/black.png" />'+
                                    '<span>客户</span>'+
                                '</div>'+
                                '<div class="memlist">';
             clientList.forEach(function(item,index){
                clientHtml +=[
                            '<div class="mblock">',
								'<img src="'+ (item.lawyerAvatar?item.lawyerAvatar:'images/default-icon.png') +'" alt="">',
								'<div class="mdesc">',
									'<p class="pf">'+item.realName+'</p>',
									'<p class="pt">'+item.lawyerType+'</p>',
								'</div>',
							'</div>'
                ].join("");
            });
            clientHtml +='</div></div>'
            memoBlockHtml += clientHtml;
        }
        if(authorizationList.length >0){
            var authorizationHtml = '<div class="teamMember">'+
                                '<div class="memname">'+
                                    '<img src="images/wtsq.png" />'+
                                    '<span>条件授权</span>'+
                                '</div>'+
                                '<div class="memlist">';
            authorizationList.forEach(function(item,index){
                authorizationHtml +=[
                            '<div class="mblock">',
								'<img src="'+ (item.lawyerAvatar?item.lawyerAvatar:'images/default-icon.png') +'" alt="">',
								'<div class="mdesc">',
									'<p class="pf">'+item.realName+'</p>',
									'<p class="pt">'+item.lawyerType+'</p>',
								'</div>',
							'</div>'
                ].join("");
            });
            authorizationHtml +='</div></div>'
            memoBlockHtml += authorizationHtml;
        }
        if(memoBlockHtml){
            $('.caseParticipant').html(memoBlockHtml)
        }else{
            $('.caseParticipant').html('<div>暂无参与人员 ):</div>')
        }
       
    }
}

//显示案件附件
YX.fn.showCaseFile = function(){
    if(this.caseDetail){
        var tempHtml = '<div class="attItems">';
        var fileList = this.caseDetail.caseDocResponseBeans;
        var enforceStageList = [],lawsuitStageList = [],applyStateList = [];
        var tempHtmlArr = []
        if(fileList.length > 0){
            fileList.forEach(function(item,index){
                if(item.docList.length > 0){
                    var stageHtml = '<div class="attTab">'+
                                        '<div class="stageName">'+item.bizStageName+'</div>'+
                                        '<div class="ithead"><span>附件名称</span> <span>大小</span> <span>上传时间</span></div>'
                        item.docList.forEach(function(e,index){
                            stageHtml += [
                                '<a href="'+e.docUrl+'" target="_blank">',
                                    '<div class="lb">',
                                            '<div class="atfir">',
                                                '<img src="'+filterFileSuffix(e.docName)+'">',
                                                '<span>'+filterOssUrl(e.docUrl)+'</span>',
                                            '</div>',
                                            '<span class="fl">'+filterFileSize(e.docSize)+'</span>',
                                            // '<span class="fl">王总</span>',
                                            '<span class="fl">'+dateStr(e.docDate)+'</span>',
                                    '</div>',
                                '</a>',
                            ].join("")
                        });
                        tempHtmlArr.push(stageHtml)
                }
            });
        }
        if(tempHtmlArr.length > 0){
            tempHtml += tempHtmlArr.join('')+'</div>'
        }else{
            tempHtml += '<div class="noCaseShow" style="margin-top: 90px;text-align:center">'+
                        '<span>暂无附件列表 ):</span>'+
                '</div></div>'
        }
        $('.attachmentContainer').html(tempHtml)
       
    }
}

//获取案件详情
YX.fn.requestGetCaseDetail = function(caseId,lawyerType){
    var _that = this;
    //开始调用赢火虫内部服务的接口
    firefly_IM_request.getRequest("get","/firefly-erp/caseInfos/allDetails/"+caseId,{},function(data){
        // console.log("案件详情=",data)
        if(data.isSuccess == "T"){
            var caseDetail = data.body;
            _that.caseDetail = caseDetail;
            var teamId =caseDetail.caseDetailBean.teamId;
            $("#case-to-parent").show();
            if(teamId){
                //先判断是否还在群中
                var teamInfo = _that.cache.getTeamById(teamId);
                if(teamInfo){
                    if(teamInfo.validToCurrentUser){
                        _that.caseToCreateteam.text("进入群聊")
                    }else{
                        _that.caseToCreateteam.text("申请进群")
                    }
                }else{
                    _that.caseToCreateteam.text("申请进群")
                }
            }else{
                //判断当前用户是否是投资经理
                var userId = sessionStorage.getItem('userId');
                if(lawyerType == "投资经理"){
                    _that.caseToCreateteam.text("创建群组")
                }else{
                    $("#case-to-parent").hide();
                }
               
            }
            _that.showCaseIntr();
        }else{
          console.log("_that===",_that)
          
        }
    },function(data){

    });
}

//去创建或是聊天界面
YX.fn.goChactBlock =function(e){
    // console.log(e.currentTarget.innerText)
    var caseId = this.caseDetail.caseDetailBean.caseId;
    var teamId =this.caseDetail.caseDetailBean.teamId;
    if(e.currentTarget.innerText == "进入群聊"){
	    this.openChatBox(teamId,'team');
    }else if(e.currentTarget.innerText == "创建群组"){
        $('#case-info-container').addClass('hide');
        this.showCreateTeamInfo(caseId,this.caseDetail.caseDetailBean.creditorName);
    }else if(e.currentTarget.innerText == "申请进群"){
        this.mysdk.applyTeam(teamId);
        var _this = this;
        setTimeout(function(){
            _this.openChatBox(teamId,'team');
        },200);
    }

}

